<!--
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-12-05 23:18:41
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2025-04-17 19:03:09
 * @FilePath: /vue-element-admin/src/views/storeSetup/product/index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!--
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-12-04 16:07:30
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-12-10 01:31:24
 * @FilePath: /vue-element-admin/src/views/storeSetup/cineraryUrn/index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="app-container">
   <!-- 搜索区 -->
   <div class="filter-container">
    <el-form :inline="true" :model="listQuery" class="demo-form-inline">
    
      <el-form-item label="物料名称">
        <el-input
          v-model="listQuery.name"
          placeholder=""
          class="filter-item"
          @keyup.enter.native="handleFilter"
        />
      </el-form-item>
      <el-form-item label="物料类型">
        <el-input
          v-model="listQuery.type"
          placeholder=""
          class="filter-item"
          @keyup.enter.native="handleFilter"
        />
      </el-form-item>
      <!-- <el-form-item label="数量">
        <el-input
          v-model="listQuery.num"
          placeholder=""
          class="filter-item"
          @keyup.enter.native="handleFilter"
        />
      </el-form-item> -->
      
      <!-- <el-form-item label="单价">
        <el-input
          v-model="listQuery.price"
          placeholder=""
          class="filter-item"
          @keyup.enter.native="handleFilter"
        />
      </el-form-item> -->

      <el-form-item label="开始日期">
        <el-date-picker
          v-model="listQuery.startDate"
          type="date"
          placeholder="选择开始日期"
          value-format="yyyy-MM-dd HH:mm:ss"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期">
        <el-date-picker
          v-model="listQuery.endDate"
          type="date"
          placeholder="选择结束日期"
          value-format="yyyy-MM-dd 23:59:59"
        >
        </el-date-picker>
      </el-form-item>


      
    </el-form>

    <!-- <el-select
      v-model="listQuery.sort"
      style="width: 140px"
      class="filter-item"
      @change="handleFilter"
    >
      <el-option
        v-for="item in sortOptions"
        :key="item.key"
        :label="item.label"
        :value="item.key"
      />
    </el-select> -->
    <el-button
      class="filter-item"
      type="primary"
      icon="el-icon-search"
      @click="handleFilter"
    >
      搜索
    </el-button>
    <el-button
      class="filter-item"
      style="margin-left: 10px"
      type="primary"
      icon="el-icon-edit"
      @click="handleCreate"
    >
      新增
    </el-button>
    <el-button
      :loading="downloadLoading"
      class="filter-item"
      type="primary"
      icon="el-icon-download"
      @click="handleDownload"
    >
      导出
    </el-button>
   
  </div>
    <el-table empty-text='暂无数据' :data="tableList" style="width: 100%;margin-top:30px;" border>
      <el-table-column align="center" label="店铺名称" width="220">
        <template slot-scope="scope">
          {{ scope.row.shopName }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="物料名称" width="220">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="物料规格" width="220">
        <template slot-scope="scope">
          {{ scope.row.spec }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="物料类型" width="220">
        <template slot-scope="scope">
          {{ scope.row.type }}
        </template>
      </el-table-column>
      
      <!-- <el-table-column align="center" label="数量" width="220">
        <template slot-scope="scope">
          {{ scope.row.num }}
        </template>
      </el-table-column> -->
      
      <el-table-column align="center" label="单价" width="220">
        <template slot-scope="scope">
          {{ scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建日期" width="220">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="备注" width="220">
        <template slot-scope="scope">
          {{ scope.row.remark }}
        </template>
      </el-table-column>
      <!-- <el-table-column align="header-center" label="Description">
        <template slot-scope="scope">
          {{ scope.row.price }}
        </template>
      </el-table-column> -->
      <el-table-column
      label="操作"
      align="center"
      width="230"
      class-name="small-padding fixed-width"
    >
      <template slot-scope="{ row, $index }">
        <el-button type="primary" size="mini" @click="handleUpdate(row)">
          编辑
        </el-button>
        <el-button
          v-if="row.status != 'deleted'"
          size="mini"
          type="danger"
          @click="handleDelete(row, $index)"
        >
          删除
        </el-button>
      </template>
    </el-table-column>
    </el-table>

 <!-- 分页区 -->
 <pagination
 v-show="total > 0"
 :total="total"
 :page.sync="listQuery.pageNum"
 :limit.sync="listQuery.limit"
 @pagination="getListQuery"
/>


<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">

  <el-form
  ref="dataForm"
  :model="temp"
  label-width="120px"
  label-position="right"
  class="demo-form-inline"
  :inline="true" 
>
        <el-form-item label="物料名称">
          <el-input v-model="temp.name" placeholder="物料名称" />
        </el-form-item>
        <el-form-item label="类型" >
          <el-select
            v-model="temp.type"
            class="filter-item"
            placeholder="类型"
          >
            <el-option
              v-for="item in typeDatas"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
      </el-form-item>
      
        <el-form-item label="规格">
          <el-select
          v-model="temp.spec"
          class="filter-item"
          placeholder="规格"
        >
          <el-option
            v-for="item in specification"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="单价">
          <el-input
            v-model="temp.price"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="单价"
          />
        </el-form-item>
        <!-- <el-form-item label="数量">
          <el-input
            v-model="temp.num"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="数量"
          />
        </el-form-item> -->
        <el-form-item label="备注">
          <el-input
            v-model="temp.remark"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消 </el-button>
        <el-button
          type="primary"
          @click="dialogStatus === 'create' ? createData() : updateData()"
        >
         确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import path from 'path'
import { deepClone } from '@/utils'
import { getListQuery, create, update, deleteData } from '@/api/settingMaterial'
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination


export default {
  components: { Pagination },

  data() {
    return {
      typeDatas:["低值易耗","固定资产","货品号"],
      specification:['个','袋','包','盒','件','张','瓶','箱'],
      tableList:[],
      listQuery: {
        pageNum: 1,
        pageSize: 20,
       
      },
      total: 0,
      listLoading: true,
      temp: {
        remark: '',
        price: '',
        name: '',
        // num:'',
        spec:''
      },
      downloadLoading: false,
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "新增",
      }
    }
  },
  computed: {
   
  },
  created() {
    this.getListQuery()
   
  },
  
  methods: {
    handleUpdate(row) {

      this.temp = Object.assign({}, row); // copy obj
      this.temp.timestamp = new Date(this.temp.timestamp);
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleDelete(row, index) {
      deleteData(row.id).then((response) => {
        this.listLoading = true
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      this.getListQuery()

      })
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
      this.listLoading = true
          const tempData = Object.assign({}, this.temp);
          tempData.timestamp = +new Date(tempData.timestamp); // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          update(tempData).then(() => {
            // this.list.splice(index, 1, this.temp);
            this.dialogFormVisible = false;
            this.$notify({
              title: "Success",
              message: "Update Successfully",
              type: "success",
              duration: 2000,
            });
      this.listLoading = false
      this.getListQuery()

          });
        }
      });
    },
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          create(this.temp).then(() => {
            this.dialogFormVisible = false;
            this.$notify({
              title: "Success",
              message: "Created Successfully",
              type: "success",
              duration: 2000,
            });
    this.getListQuery()

          });
        }
      });
    },
    resetTemp() {
      this.temp = {
        remark: '',
        price: '',
        name: '',
        // num:''

      };
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    getListQuery() {
      this.listLoading = true
      getListQuery(this.listQuery).then((response) => {
        this.tableList = response.data.records
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getListQuery()
    },
    handleAdd(){

    },
    formatJson(filterVal) {
      return this.tableList.map((v) =>
        filterVal.map((j) => {
          if (j === "timestamp") {
            return parseTime(v[j]);
          } else {
            return v[j];
          }
        })
      );
    },
    handleDownload() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then((excel) => {
        const tHeader = ["物料", "价格","创建时间",'备注'];
        const filterVal = [
          "name","price","createTime", "remark"
        ];
        const data = this.formatJson(filterVal);
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: "table-list",
        });
        this.downloadLoading = false;
      });
    },
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
